<?php

/**
 * lzp300 2015-01-27
 */
class hotel extends actionAbstract {

    /**
     * 构造
     */
    function __construct() {
        parent::__construct();
        if (!isset($_COOKIE['qingMg_admininfo'])) {
            header('location:' . $this->url('common/login'));
        }
    }

    //酒店列表
    function index() {
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';

        $sql_from = " from qm_hotel ";
        $sql_where = "";
        $sql_order_by = " order by created desc";
        $field_reto = " count(*)";
        $field_re = " * ";

        if ($keywords) {
            $keywordsArr = explode(',', $keywords);
            $sql_where.=" where (";
            foreach ($keywordsArr as $k => $v) {
                if ($k == 0) {
                    $sql_where.="name like '%" . trim($v) . "%' or hid like '%" . trim($v) . "%'";
                } else {
                    $sql_where.=" or name like '%" . trim($v) . "%' or hid like '%" . trim($v) . "%'";
                }
            }
            $sql_where.=")";
        }

        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "qm", "hotel", $page, 15);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }

    //酒店首页显示操作
    function isindex() {
        $this->loadModel('hotel', 'category');
        $data['isindex'] = isset($_POST['isindex']) ? (int) ($_POST['isindex']) : 0;
        $kid = isset($_POST['kid']) ? (int) ($_POST['kid']) : 0;
        $re = $this->hotel->categoryModel->update($data, "id=$kid");
        if ($re) {
            exit(json_encode(array('state' => 1, 'html' => '修改成功')));
        } else {
            exit(json_encode(array('state' => 0, 'html' => '修改失败')));
        }
    }

    //酒店上下架操作
    function grounding() {
        $this->loadModel('hotel', 'category');
        $data['grounding'] = isset($_POST['grounding']) ? (int) ($_POST['grounding']) : 0;
        $kid = isset($_POST['kid']) ? (int) ($_POST['kid']) : 0;
        $re = $this->hotel->categoryModel->update($data, "id=$kid");
        if ($re) {
            exit(json_encode(array('state' => 1, 'html' => '修改成功')));
        } else {
            exit(json_encode(array('state' => 0, 'html' => '修改失败')));
        }
    }

    //房型列表
    function main() {
        $this->loadModel('qm', 'hotel');
        $this->loadModel('qm', 'room');
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
        
        $sql_from = " from qm_room as a left join qm_hotel as b on b.hid=a.hid";
        $sql_where = "";
        $sql_order_by = " order by a.created desc";
        $field_reto = " count(*)";
        $field_re = " a.*,b.name as hname";

        if ($keywords) {
            $keywordsArr = explode(',', $keywords);
            $sql_where.=" where (";
            foreach ($keywordsArr as $k => $v) {
                if ($k == 0) {
//                    $sql_where.="b.hotelName like '%" . trim($v) . "%' or a.hotelId like '%" . trim($v) . "%'" . " or a.title like '%" . trim($v) . "%'" . " or a.typeId like '%" . trim($v) . "%'";
                    $sql_where.="b.name like '%" . trim($v) . "%' or a.hid like '%" . trim($v) . "%'" . " or a.name like '%" . trim($v) . "%'" . " or a.rid like '%" . trim($v) . "%'";
                } else {
                    $sql_where.=" or b.name like '%" . trim($v) . "%' or a.hid like '%" . trim($v) . "%'" . " or a.name like '%" . trim($v) . "%'" . " or a.rid like '%" . trim($v) . "%'";
//                    $sql_where.=" or b.hotelName like '%" . trim($v) . "%' or a.hotelId like '%" . trim($v) . "%'" . " or a.title like '%" . trim($v) . "%'" . " or a.typeId like '%" . trim($v) . "%'";
                }
            }
            $sql_where.=")";
        }

        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "qm", "room", $page, 15);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }

    //房价列表
    function price() {
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';

        $start = isset($_GET['start']) ? trim($_GET['start']) : '';
        $end = isset($_GET['end']) ? trim($_GET['end']) : '';
        if ($start && $end && (strtotime($end) < strtotime($start))) {
            $start = $_GET['end'];
            $end = $_GET['start'];
        }
        
        $sql_from =" from qm_roombatch as a left join qm_hotel as b on b.hid = a.hid left join qm_room as c on c.rid = a.rid";
        $sql_where = "";
        $sql_order_by = " order by a.hid,a.rid,a.date desc";
        $field_reto = " count(*)";
        $field_re = " a.*,b.name as hname,c.name as rname ";
        

//        $sql_from = " from hotel_price as a left join hotel_category as b on b.hotelId=a.hotelId left join hotel_type as c on c.hotelId=a.hotelId and c.typeId=a.typeId";
//        $sql_where = "";
//        $sql_order_by = " order by a.hotelId,a.typeId,a.priceDate desc";
//        $field_reto = " count(*)";
//        $field_re = " a.*,b.hotelName,c.title ";

        if ($keywords) {
            $keywordsArr = explode(',', $keywords);
            $sql_where.=" where (";
            foreach ($keywordsArr as $k => $v) {
                if ($k == 0) {
                    $sql_where.="b.name like '%" . trim($v) . "%' or a.hid like '%" . trim($v) . "%'" . " or c.name like '%" . trim($v) . "%'" . " or a.rid like '%" . trim($v) . "%'";
                } else {
                    $sql_where.=" or b.name like '%" . trim($v) . "%' or a.hid like '%" . trim($v) . "%'" . " or c.name like '%" . trim($v) . "%'" . " or a.rid like '%" . trim($v) . "%'";
                }
            }
            $sql_where.=")";
        }

        if ($start && $sql_where) {
            $sql_where.=" and a.date>='" . ($start) . "'";
        } elseif ($start && !$sql_where) {
            $sql_where = " where a.date>='" . ($start) . "'";
        }

        if ($end && $sql_where) {
            $sql_where.=" and a.date<='" . ($end) . "'";
        } elseif ($end && !$sql_where) {
            $sql_where = " where a.date<='" . ($end) . "'";
        }

        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "qm", "batch", $page, 15);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }

    //房态列表
    function info() {
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';

        $start = isset($_GET['start']) ? trim($_GET['start']) : '';
        $end = isset($_GET['end']) ? trim($_GET['end']) : '';
        if ($start && $end && (strtotime($end) < strtotime($start))) {
            $start = $_GET['end'];
            $end = $_GET['start'];
        }
        $sql_from =" from qm_roombatch as a left join qm_hotel as b on b.hid = a.hid left join qm_room as c on c.rid = a.rid";
        $sql_where = "";
        $sql_order_by = " order by a.hid,a.rid,a.date desc";
        $field_reto = " count(*)";
        $field_re = " a.*,b.name as hname,c.name as rname ";
        

//        $sql_from = " from hotel_typestatus as a left join hotel_category as b on b.hotelId=a.hotelCode left join hotel_type as c on c.hotelId=a.hotelCode and c.typeId=a.roomID";
//        $sql_where = "";
//        $sql_order_by = " order by a.hotelCode,a.roomID,a.times desc";
//        $field_reto = " count(*)";
//        $field_re = " a.*,b.hotelName,c.title ";

//        if ($keywords) {
//            $keywordsArr = explode(',', $keywords);
//            $sql_where.=" where (";
//            foreach ($keywordsArr as $k => $v) {
//                if ($k == 0) {
//                    $sql_where.="b.hotelName like '%" . trim($v) . "%' or a.hotelCode like '%" . trim($v) . "%'" . " or c.title like '%" . trim($v) . "%'" . " or a.roomID like '%" . trim($v) . "%'";
//                } else {
//                    $sql_where.=" or b.hotelName like '%" . trim($v) . "%' or a.hotelCode like '%" . trim($v) . "%'" . " or c.title like '%" . trim($v) . "%'" . " or a.roomID like '%" . trim($v) . "%'";
//                }
//            }
//            $sql_where.=")";
//        }
        
        if ($keywords) {
            $keywordsArr = explode(',', $keywords);
            $sql_where.=" where (";
            foreach ($keywordsArr as $k => $v) {
                if ($k == 0) {
                    $sql_where.="b.name like '%" . trim($v) . "%' or a.hid like '%" . trim($v) . "%'" . " or c.name like '%" . trim($v) . "%'" . " or a.rid like '%" . trim($v) . "%'";
                } else {
                    $sql_where.=" or b.name like '%" . trim($v) . "%' or a.hid like '%" . trim($v) . "%'" . " or c.name like '%" . trim($v) . "%'" . " or a.rid like '%" . trim($v) . "%'";
                }
            }
            $sql_where.=")";
        }

        if ($start && $sql_where) {
            $sql_where.=" and a.date>='" . ($start) . "'";
        } elseif ($start && !$sql_where) {
            $sql_where = " where a.date>='" . ($start) . "'";
        }

        if ($end && $sql_where) {
            $sql_where.=" and a.date<='" . ($end) . "'";
        } elseif ($end && !$sql_where) {
            $sql_where = " where a.date<='" . ($end) . "'";
        }
        
        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "qm", "roombatch", $page, 15);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }

    //图片列表
    function pictrure() {
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';

        $sql_from = " from qm_image as a left join qm_hotel as b on b.hid=a.hid";
        $sql_where = "";
        $sql_order_by = " order by a.hid,a.created desc";
        $field_reto = " count(*)";
        $field_re = " a.*,b.name as hname ";

        if ($keywords) {
            $keywordsArr = explode(',', $keywords);
            $sql_where.=" where (";
            foreach ($keywordsArr as $k => $v) {
                if ($k == 0) {
                    $sql_where.="b.name like '%" . trim($v) . "%' or a.title like '%" . trim($v) . "%'";
                } else {
                    $sql_where.=" or b.name like '%" . trim($v) . "%' or a.title like '%" . trim($v) . "%'";
                }
            }
            $sql_where.=")";
        }

        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "qm", "image", $page, 15);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }

    //删除图片
    function delPictrure() {
        $kid = isset($_POST['kid']) ? (int) ($_POST['kid']) : 0;
        $this->loadModel('hotel', 'picture');
        $re = $this->hotel->pictureModel->delete("id=$kid");
        if ($re) {
            exit(json_encode(array('state' => 1, 'html' => '删除成功，2秒后自动刷新')));
        } else {
            exit(json_encode(array('state' => 0, 'html' => '删除失败')));
        }
    }

    //设施设备列表
    function devices() {
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';

        $sql_from = " from hotel_devices as a left join hotel_category as b on b.hotelId=a.hotelId";
        $sql_where = "";
        $sql_order_by = " order by a.hotelId,a.created desc";
        $field_reto = " count(*)";
        $field_re = " a.*,b.hotelName ";

        if ($keywords) {
            $keywordsArr = explode(',', $keywords);
            $sql_where.=" where (";
            foreach ($keywordsArr as $k => $v) {
                if ($k == 0) {
                    $sql_where.="b.hotelName like '%" . trim($v) . "%' or a.title like '%" . trim($v) . "%'";
                } else {
                    $sql_where.=" or b.hotelName like '%" . trim($v) . "%' or a.title like '%" . trim($v) . "%'";
                }
            }
            $sql_where.=")";
        }

        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "hotel", "devices", $page, 10);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }

    //评论列表
    function comment() {
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';

        $sql_from = " from qm_comment as a left join qm_hotel as b on b.hid=a.hid";
        $sql_where = "";
        $sql_order_by = " order by a.hid,a.created desc";
        $field_reto = " count(*)";
        $field_re = " a.*,b.name as hname";

        if ($keywords) {
            $keywordsArr = explode(',', $keywords);
            $sql_where.=" where (";
            foreach ($keywordsArr as $k => $v) {
                if ($k == 0) {
                    $sql_where.="b.name like '%" . trim($v) . "%' or b.hid like '%" . trim($v) . "%'";
                } else {
                    $sql_where.=" or b.name like '%" . trim($v) . "%' or b.hid like '%" . trim($v) . "%'";
                }
            }
            $sql_where.=")";
        }

        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "qm", "comment", $page, 15);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }

}
